﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using NoSqlTest.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NoSqlTest.EfContext
{
    public class EfDbContext : DbContext
    {
        /// <summary>
        /// 指定静态ILoggerFactory
        /// </summary>
        public static readonly ILoggerFactory MyLoggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); });

        public EfDbContext() { }

        public EfDbContext(DbContextOptions<EfDbContext> options)
            : base(options)
        {
        }

        private string Conn = null;
        public DbContext ToWriteOrRead(string conn)
        {
            Conn = conn;
            return this;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder
                    //.UseLoggerFactory(MyLoggerFactory)//打印Sql
                    //.UseLazyLoadingProxies()
                    .UseSqlServer(Conn);
            }
            //optionsBuilder.UseLoggerFactory(MyLoggerFactory);//打印Sql
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            #region MyRegion

            {
                //指定主键
                //modelBuilder.Entity<ClassGrade>().HasKey(a => a.Id);
                /////设置数据库架构
                //modelBuilder.HasDefaultSchema("xl");
                /////表名、属性名映射
                //modelBuilder.Entity<UserInfo>().ToTable("UserInfos", "Zhaoxi").Property(p => p.UserAge).HasColumnName("Age");
                ////设置联合主键
                //modelBuilder.Entity<SysUserRoleMapping>().HasKey(p => new { p.SysUserId, p.SysRoleId }); 
                ////初始化数据
                //modelBuilder.Entity<Company>().HasData(new List<Company>()
                //{
                //});
                ///////表拆分:在数据库中是一整张表，在代码层面是多个实体与其对应；
                //modelBuilder.Entity<SysLog>(dob =>
                //{
                //    dob.ToTable("SysLogInfo");
                //    dob.Property(o => o.LogType).HasColumnName("LogType");//配置两个实体的相同属性映射到表的同一列
                //    dob.HasOne(o => o.SysLogDetail).WithOne().HasForeignKey<SysLog>(o => o.Id); ; //配置两个实体的相同属性映射到表的同一列
                //});
                //modelBuilder.Entity<SysLogDetail>(dob =>
                //{
                //    dob.ToTable("SysLogInfo");
                //    dob.Property(o => o.LogType).HasColumnName("LogType");//配置两个实体的相同属性映射到表的同一列 
                //});
            }

            //设置一对多的关系
            modelBuilder.Entity<Student>().HasOne(c => c.Class).WithMany(s => s.Students).HasForeignKey(b => b.ClassId);

            ////多对多关系  
            modelBuilder.Entity<MiddleStudentCourse>(eb =>
            {
                eb.HasOne(p => p.Course).WithMany(u => u.Students).HasForeignKey(u => u.CourseId);
                eb.HasOne(p => p.Student).WithMany(r => r.Courses).HasForeignKey(s => s.StudentId);
            }); 
            modelBuilder.Entity<MiddleClassCourse>(eb => {
                eb.HasOne(p => p.Course).WithMany(u => u.ClassStudents).HasForeignKey(u => u.CourseId);
                eb.HasOne(p => p.Class).WithMany(r => r.Classs).HasForeignKey(s => s.ClassId);
            });
            #endregion
        }


        public DbSet<ClassGrade> Classs { get; set; }
        public DbSet<Student> Students { get; set; }
        public DbSet<Course> Courses { get; set; }
    }
}
